package org.atlantis.grosbeak.cable.template;

import java.io.OutputStream;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 用来渲染一段字节的模板。（通常用于文件下载）
 * 
 * @author <a href="mailto:seenoevil.cn@gmail.com">SeeNoEvil</a>
 * 
 */
public class BinaryTemplate implements Template {

	private byte[] data;

	public BinaryTemplate(byte[] data) {
		this.data = data;
	}

	public void render(ServletContext sc, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		response.setContentType("application/octet-stream");
		response.setContentLength(data.length);
		OutputStream ops = response.getOutputStream();
		ops.write(data);
		ops.flush();
		ops.close();
	}
}
